package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.common.CoustomException;
import com.example.demo.entity.Category;
import com.example.demo.entity.Dish;
import com.example.demo.entity.Setmeal;
import com.example.demo.mapper.CategoryMapper;
import com.example.demo.service.CategoryService;
import com.example.demo.service.DishService;
import com.example.demo.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Override
    public void delete(Long ids) {
        //添加查询条件，查询当前菜品是否关联菜系
        LambdaQueryWrapper<Dish> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Dish::getCategoryId,ids);
        int count = dishService.count(lambdaQueryWrapper);
        if (count>0){
            //已关联菜系
            throw  new CoustomException("该菜品关联菜系不能删除");
        }
        //添加查询条件，查询当前套餐是否关联菜系
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper1=new LambdaQueryWrapper();
        lambdaQueryWrapper1.eq(Setmeal::getCategoryId,ids);
        count = setmealService.count(lambdaQueryWrapper1);
        if (count>0){
            //已关联菜系
            throw  new CoustomException("该套餐关联菜系不能删除");
        }
        super.removeById(ids);
    }
}
